package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbBrand;
import com.bwie.service.TbBrandService;
import com.bwie.mapper.TbBrandMapper;
import com.bwie.utils.Result;
import com.bwie.vo.PageInfoVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
* @author 31803
* @description 针对表【tb_brand】的数据库操作Service实现
* @createDate 2025-05-23 13:51:23
*/
@Service
public class TbBrandServiceImpl extends ServiceImpl<TbBrandMapper, TbBrand>
    implements TbBrandService{

    @Autowired
    TbBrandMapper tbBrandMapper;
    @Autowired
    HttpServletRequest request;

    @Override
    public Result AddBrand(TbBrand tbBrand) {

        TbBrand tbBrand1 = tbBrandMapper.selectOne(
                new QueryWrapper<TbBrand>().lambda().eq(TbBrand::getBrandName, tbBrand.getBrandName())
        );
        if(tbBrand1!=null){
            return Result.error().setCode(500).setMsg("品牌名称重复了");
        }
        tbBrand.setCreateTime(new Date());
        String token = request.getHeader("token");
        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        String nickName = body.get("userNick", String.class);
        tbBrand.setUserCount(0);
        tbBrand.setCreateBy(nickName);
        tbBrandMapper.insert(tbBrand);
        return Result.ok();
    }

    @Override
    public Result BrandList(PageInfoVo pageInfoVo) {
        Page<TbBrand> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        QueryWrapper<TbBrand> wrapper = new QueryWrapper<>();

        if(StringUtil.isNotEmpty(pageInfoVo.getKeyWord())){
            wrapper.lambda().like(TbBrand::getBrandName,pageInfoVo.getKeyWord());
        }
       wrapper.lambda().orderByDesc(TbBrand::getCreateTime);
        Page<TbBrand> tbBrandPage = tbBrandMapper.selectPage(page, wrapper);
        return Result.ok().setData(tbBrandPage);
    }

    @Override
    public Result updateStatus(TbBrand tbBrand) {
        //判断品牌存不存在
        TbBrand brand = tbBrandMapper.selectOne(
                new QueryWrapper<TbBrand>().lambda().eq(TbBrand::getBrandName, tbBrand.getBrandName())
        );
        if(brand==null){
            return Result.error().setCode(500).setMsg("用户品牌不存在");
        }
        brand.setBrandStatus(tbBrand.getBrandStatus());
        brand.setUpdateTime(new Date());
        String token = request.getHeader("token");
        if(token==null||token==""){
            Result.error().setCode(500).setMsg("token不存在请先登录");
        }
        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        String userNick = body.get("userNick", String.class);
        brand.setUpdateBy(userNick);
        tbBrandMapper.updateById(brand);
        return Result.ok();
    }

    @Override
    public Result TbbrandList() {
        List<TbBrand> tbBrands = tbBrandMapper.selectList(null);
        return Result.ok().setData(tbBrands);
    }
}




